home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / GAMES_1 / CHESSCLK.ZIP / CHESSCLK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-15  |  7.1 KB  |  276 lines

  1. /****************************** Chess clock program ************************
  2. Full-featured chess clock: Digital displays of time
  3.                Move counter
  4.                White and Black clocks separately settable
  5.                Times from 1 minute to 99 hours
  6.                Low-time warning (optional)
  7.                Flashing "tick" indicator (optional)
  8. ****************************************************************************/
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <graphics.h>
  16. #include <dos.h>
  17. #include "oscr.hpp"
  18. #include "chessclk.hpp"
  19.  
  20. void main()
  21. {
  22.    randomize();
  23.    opening_screen();
  24.    play();
  25. }
  26.  
  27. void CountdownTimer::clock_on()
  28. {
  29.    time_t prev_sec;
  30.    int ch;
  31.  
  32.       if( p == WHITE_ )
  33.      text_color = LIGHTBLUE;
  34.       else
  35.      text_color = DARKGRAY;
  36.  
  37.       setcolor( text_color );
  38.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  39.       settextjustify( LEFT_TEXT, TOP_TEXT );
  40.  
  41.       startn_t = time ( NULL );  //Click on stopwatch.
  42.       gotoxy( 60 * p + 1, 14 );
  43.       outtextxy( NAME_POS * p, 100, player [p] );
  44.       display_time();  //Otherwise initial time not displayed...
  45.  
  46.       while( !( ch = kbhit() ) )
  47.      {
  48.      prev_sec = seconds;
  49.      if( running_flag) //Is this 2nd or later lap?
  50.         interval_t = time( NULL ) - startn_t;
  51.      else
  52.         interval_t = time( NULL ) - start_t;
  53.      running_t = total_seconds - interval_t;
  54.      convert( running_t );
  55.  
  56.      if( seconds - prev_sec )
  57.         {
  58.         display_time();
  59.  
  60.         if( !seconds )
  61.            if( minutes == warning )
  62.           if( !hours )
  63.              if( time_warning_flag )
  64.             blatt();
  65.  
  66.         if( visual_ticking_flag ) // Show blinking box ticks?
  67.            {
  68.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  69.            setcolor ( random ( COLORS ) );
  70.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  71.            bar( X_C - RADIUS, Y_C - RADIUS,
  72.             X_C + RADIUS, Y_C + RADIUS );
  73.            }
  74.         }
  75.  
  76.      if( timeout() )
  77.         exit_();
  78.      }
  79.  
  80.       ch = getch();
  81.       if( ch == ESC )
  82.      exit__();  // Quit.
  83.  
  84.       total_seconds = hours * 3600 + minutes * 60 + seconds;
  85.       running_flag = ON;  // Remember that clock was already running.
  86.  
  87.       return;
  88.  
  89. }
  90.  
  91. void CountdownTimer::display_moves()
  92. {
  93.    char buf[ 5 ];
  94.    static char ebuf[ 5 ];
  95.  
  96.       if( moves > 1 )
  97.      {
  98.      setcolor( WHITE );
  99.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  100.      settextjustify( CENTER_TEXT, TOP_TEXT );
  101.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  102.      }
  103.  
  104.       sprintf( buf, "%003d", moves );
  105.       sprintf( ebuf, buf );
  106.       setcolor( LIGHTRED );
  107.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  108.       settextjustify( CENTER_TEXT, TOP_TEXT );
  109.       outtextxy( MOVES_X, MOVES_Y, buf );
  110.       
  111.       return;
  112. }
  113.  
  114. void graphics_setup( int background_color )
  115. {
  116.    int grdriver = VGA,
  117.        grmode = VGAHI;
  118.  
  119.        registerfarbgidriver( EGAVGA_driver_far );
  120.        registerfarbgifont( gothic_font_far );
  121.        registerfarbgifont( triplex_font_far );
  122.        initgraph( &grdriver, &grmode, "" );
  123.        setbkcolor( background_color );
  124.  
  125. }
  126.  
  127. void exit__()
  128. {
  129.       closegraph();
  130.       exit( QUIT );
  131. }
  132.  
  133.     /***************Routine to erase old numbers*************/
  134. void CountdownTimer::erase_numbers()
  135. {
  136.       setcolor ( WHITE ); 
  137.  
  138.       if( seconds == 59 )
  139.      outtextxy( p * BLK_TIME, Y_TIMEPOS, line_clear );
  140.       else
  141.      if( seconds == 9 || seconds == 19 || seconds == 29
  142.          || seconds == 39 || seconds == 49 )
  143.         outtextxy( p * BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  144.                line_clear + 6 );
  145.       else
  146.          outtextxy( p * BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  147.             line_clear + 7 ); 
  148.  
  149.      return;
  150.  
  151. }
  152.  
  153.  
  154. void play()
  155. {
  156.    int hrs,
  157.        min;
  158.    char inputstr[ MAXLEN ],
  159.     inp;
  160.  
  161.       clrscr();
  162.  
  163.       textcolor ( LIGHTCYAN );
  164.       cprintf( "\n                              WHITE hours: " );
  165.       gets( inputstr );
  166.       hrs = atoi( inputstr );
  167.       cprintf( "                              WHITE minutes: " );
  168.       gets( inputstr );
  169.       min = atoi( inputstr );
  170.       CountdownTimer t1( hrs, min, WHITE_ );
  171.  
  172.       textcolor( RED );
  173.       cprintf( "\n                              BLACK hours: " );
  174.       gets( inputstr );
  175.       hrs = atoi( inputstr );
  176.       cprintf( "                              BLACK minutes: " );
  177.       gets( inputstr );
  178.       min = atoi( inputstr );
  179.       CountdownTimer t2( hrs, min, BLACK_ );
  180.  
  181.       textcolor( YELLOW );
  182.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  183.       inp = getche();
  184.       if( inp == 'y' || inp == 'Y' )
  185.      t1.visual_ticking_flag = t2.visual_ticking_flag = ON;
  186.       else
  187.      t1.visual_ticking_flag = t2.visual_ticking_flag = OFF;
  188.  
  189.       cprintf( "\n                                                      Enable time warning? " );
  190.       inp = getche();
  191.       if( inp == 'y' || inp == 'Y' )
  192.      {
  193.      t1.time_warning_flag = t2.time_warning_flag = ON;
  194.      cprintf( "                                                          At how many minutes? " );
  195.      gets( inputstr );
  196.      t1.warning = t2.warning = atoi( inputstr );
  197.      }
  198.       else
  199.      t1.time_warning_flag = t2.time_warning_flag = OFF;
  200.  
  201.       textcolor( GREEN | BLINK );
  202.       _setcursortype( _NOCURSOR );
  203.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  204.       cprintf( "                             PRESS A KEY TO BEGIN" );
  205.       while ( !getch() );
  206.  
  207.       graphics_setup( WHITE );
  208.  
  209.       t1.initialize_clock();
  210.       t1.moves++;
  211.       t1.display_moves();
  212.       t1.clock_on();
  213.       t2.initialize_clock();
  214.       t2.clock_on();
  215.  
  216.       while ( PLAY )
  217.      {
  218.      t1.moves++;  
  219.      t1.display_moves();
  220.      t1.clock_on();
  221.      t2.clock_on(); 
  222.      }
  223.  
  224. } // End play()
  225.  
  226.  
  227. void opening_screen()
  228. {
  229.    char topline[] = "Chess Clock",
  230.     by_line[] = "by",
  231.     name_line[] = "M\\Cooper",
  232.     endline[] = "PRESS A KEY";
  233.  
  234.       graphics_setup( CYAN );
  235.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  236.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  237.       setcolor( MAGENTA );
  238.       outtextxy( TOPX, TOPY, topline );
  239.  
  240.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  241.       setcolor( BLUE );
  242.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  243.  
  244.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  245.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  246.  
  247.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  248.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  249.       pieslice( PIE2_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  250.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  251.       circle( PIE2_X, PIE_Y, CIRC_RAD );
  252.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  253.       line( LINE2_X, LINE_Y1, LINE2_X, LINE2_Y2 );
  254.       setfillstyle( PIE_PATTERN, WHITE );
  255.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  256.       bar( B2_LEFT, B2_TOP, B2_RIGHT, B_BOTTOM );
  257.  
  258.  
  259.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  260.       setcolor( BLUE );
  261.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  262.  
  263.       sleep( DELAY );
  264.  
  265.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  266.       setcolor( RED );
  267.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  268.  
  269.       getch();
  270.       closegraph();
  271.  
  272.       return;
  273. }
  274.  
  275.  
  276.